import { Scene, Types } from 'phaser';
import { PlayGround } from '@/pages/game/TetrisLike/classes/PlayGround';
import { Block } from '@/pages/game/TetrisLike/classes/Block';

import wall from '../../../../assets/resources/TetrisLike/slime_stone0.png';
import floor from '../../../../assets/resources/TetrisLike/crypt_domino_8a.png';
import crystal0 from '../../../../assets/resources/TetrisLike/crystal/crystal_wall_white.png';
import crystal1 from '../../../../assets/resources/TetrisLike/crystal/crystal_wall_yellow.png';
import crystal2 from '../../../../assets/resources/TetrisLike/crystal/crystal_wall_green.png';
import crystal3 from '../../../../assets/resources/TetrisLike/crystal/crystal_wall_cyan.png';
import crystal4 from '../../../../assets/resources/TetrisLike/crystal/crystal_wall_red.png';
import crystal5 from '../../../../assets/resources/TetrisLike/crystal/crystal_wall_lightblue.png';
import crystal6 from '../../../../assets/resources/TetrisLike/crystal/crystal_wall_blue.png';

const resources = {
  wall,
  floor,
  crystal0,
  crystal1,
  crystal2,
  crystal3,
  crystal4,
  crystal5,
  crystal6,
};

const grid = { size: 32, width: 12, height: 21 };
const key = {
  ground: ['wall', 'floor'],
  block: ['crystal0', 'crystal1', 'crystal2', 'crystal3', 'crystal4', 'crystal5', 'crystal6'],
};

class TetrisLikeScene extends Scene {
  cursors!: Types.Input.Keyboard.CursorKeys;
  ground!: PlayGround;
  block!: Block;

  score = 0;

  constructor() {
    super('TetrisLike');
  }

  preload() {
    this.load.setBaseURL(window.location.origin);
    Object.keys(resources).forEach(k => this.load.image(k, `${resources[k]}`));
  }

  create() {
    this.ground = new PlayGround(this, { grid, key: key.ground });

    this.block = new Block(this, { grid, key: key.block });
    this.block.create();
    this.block.place();

    this.cursors = this.input.keyboard.createCursorKeys();
  }

  update(time: number) {
    this.handleDrop(time);
    this.handleMove(time);
  }

  handleDrop(time: number) {
    if (this.block.DropTime(time)) {
      if (!this.block.move(0, 1)) {
        // eslint-disable-next-line no-constant-condition
        while (1) {
          const count = this.block.checkAndSweep();
          if (count > 0) {
            this.block.shift();
            this.score += count;
          } else {
            break;
          }
        }
        this.block.create();
        if (!this.block.place()) {
          // Game Over
          this.scene.pause();
        }
      }
    }
  }

  handleMove(time: number) {
    if (this.cursors.up!.isDown) {
      if (this.block.MoveTime(time)) {
        this.block.rotate();
      }
    } else if (this.cursors.down!.isDown) {
      if (this.block.MoveTime(time)) {
        this.block.move(0, 1);
      }
    } else if (this.cursors.left!.isDown) {
      if (this.block.MoveTime(time)) {
        this.block.move(-1, 0);
      }
    } else if (this.cursors.right!.isDown) {
      if (this.block.MoveTime(time)) {
        this.block.move(1, 0);
      }
    }
  }
}

export default TetrisLikeScene;
